<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "edu_active_pay".
 *
 * @property int $id
 * @property string $openid 用户openid
 * @property string $out_trade_no 支付订单表
 * @property string $total_fee 支付总金额
 * @property string $transaction_id 支付金额
 * @property int $status 0代表未支付 1代表支付
 * @property int $create_time 支付完成时间
 */
class EduActivePay extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'edu_active_pay';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['openid', 'out_trade_no', 'total_fee', 'transaction_id', 'create_time'], 'required'],
            [['total_fee'], 'number'],
            [['status', 'create_time'], 'integer'],
            [['openid', 'out_trade_no', 'transaction_id'], 'string', 'max' => 60],
            [['out_trade_no'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'openid' => 'Openid',
            'out_trade_no' => 'Out Trade No',
            'total_fee' => 'Total Fee',
            'transaction_id' => 'Transaction ID',
            'status' => 'Status',
            'create_time' => 'Create Time',
        ];
    }

    /**
     * @return array
     * 获取今天的交易额
     */
    public static function todayTrading()
    {
        $start_time = strtotime(date("Y-m-d 00:00:00"));
        $end_time = strtotime(date("Y-m-d 23:59:59"));
        return (new \yii\db\Query())
            ->select(['SUM(total_fee) as amount', 'COUNT(*) as count'])
            ->from('edu_active_pay')
            ->where(['>', 'create_time', $start_time])
            ->andWhere(['<', 'create_time', $end_time])
            ->andWhere(['status'=> 1])
            ->all();
    }
}
